<template>
  <div class="my-container">
    <!-- 已登录 -->
    <div v-if="user" class="header user-info">
      <!-- 用户信息 -->
      <div class="base-info">
        <div class="left">
          <van-image class="avatar" round fit="cover" :src="userinfo.photo" />
          <span class="name">{{ userinfo.name }}</span>
        </div>
        <div class="right">
          <van-button size="mini" round>编辑资料</van-button>
        </div>
      </div>
      <!-- 用户数据 -->
      <div class="data-stats">
        <div class="data-item">
          <span class="count">{{ userinfo.art_count }}</span>
          <span class="text">头条</span>
        </div>
        <div class="data-item">
          <span class="count">{{ userinfo.follow_count }}</span>
          <span class="text">关注</span>
        </div>
        <div class="data-item">
          <span class="count">{{ userinfo.like_count }}</span>
          <span class="text">喜欢</span>
        </div>
        <div class="data-item">
          <span class="count">{{ userinfo.fans_count }}</span>
          <span class="text">粉丝</span>
        </div>
      </div>
    </div>
    <!-- 未登录 -->
    <div v-else class="header not-login">
      <div class="login-btn" @click="$router.push('/login')">
        <img class="mobile-img" src="@/assets/mobile.png" />
        <span class="text">登录&nbsp;/&nbsp;注册</span>
      </div>
    </div>
    <!-- 宫格导航 -->
    <van-grid :column-num="2">
      <van-grid-item icon-prefix="toutiao" icon="shoucang" text="收藏" />
      <van-grid-item icon-prefix="toutiao" icon="lishi" text="历史" />
    </van-grid>
    <!-- 消息通知 -->
    <van-cell title="消息通知" is-link />
    <van-cell title="小智同学" is-link />
    <van-cell v-if="user" class="logout" title="退出登录" @click="logout" />
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { userApi } from '@/api/User'
export default {
  data() {
    return {
      userinfo: {} // 存储用户信息
    }
  },
  computed: {
    ...mapState(['user'])
  },
  created() {
    // 判断如果存储了token的情况下就去获取用户信息
    if (this.user) {
      this.getUserInfo()
    }
  },
  methods: {
    // 退出登录
    async logout() {
      // this.$dialog
      //   .confirm({
      //     title: '标题',
      //     message: '弹窗内容'
      //   })
      //   .then(() => {
      //     // on confirm
      //     console.log('点击确认')
      //   })
      //   .catch(() => {
      //     // on cancel
      //     console.log('点击取消')
      //   })
      try {
        await this.$dialog.confirm({
          title: '退出登录',
          message: '确认要退出登录吗？'
        })
        // 退出登录逻辑处理
        this.$store.commit('setUserToken', null)
      } catch (error) {
        console.log(error);
      }
    },
    // 获取用户信息
    async getUserInfo() {
      const res = await userApi()
      console.log(res)
      this.userinfo = res.data.data
    }
  }
}
</script>

<style lang="less">
.my-container {
  .header {
    height: 361px;
    background: url('~@/assets/banner.png');
    background-size: cover;
  }
  .not-login {
    display: flex;
    justify-content: center;
    align-items: center;
    .login-btn {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      .mobile-img {
        width: 132px;
        height: 132px;
        margin-bottom: 15px;
      }
      .text {
        font-size: 28px;
        color: #fff;
      }
    }
  }
  .user-info {
    .base-info {
      height: 231px;
      padding: 76px 32px 23px;
      box-sizing: border-box;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .left {
        display: flex;
        align-items: center;
        .avatar {
          width: 132px;
          height: 132px;
          border: 4px solid #fff;
          margin-right: 23px;
        }
        .name {
          font-size: 30px;
          color: #fff;
        }
      }
    }
    .data-stats {
      display: flex;
      .data-item {
        height: 130px;
        flex: 1;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        color: #fff;
        .count {
          font-size: 36px;
        }
        .text {
          font-size: 23px;
        }
      }
    }
  }
  .van-grid {
    .toutiao {
      color: #eb5253;
    }
  }
  .logout {
    .van-cell__title {
      text-align: center;
      color: #d86262;
    }
  }
}
</style>